package edu.whu.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.whu.demo.domain.Supplier;
import edu.whu.demo.exception.ProductAdminException;
import edu.whu.demo.service.impl.SupplierServiceImpl;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yang hengyi
 * @since 2022-10-24
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    private SupplierServiceImpl supplierService;

    @ApiOperation("根据Id查找供应商")
    @GetMapping("/{id}")
    public ResponseEntity<Supplier> getSupplier(@PathVariable Long id){
        Supplier supplier = supplierService.getById(id);
        if(supplier == null) {
            return ResponseEntity.noContent().build();
        }
        else {
            return ResponseEntity.ok(supplier);
        }
    }

    @ApiOperation("添加供货商")
    @PostMapping("")
    public ResponseEntity<Supplier> addSupplier(@RequestBody Supplier supplier) throws ProductAdminException {
        return ResponseEntity.ok(supplierService.addSupplier(supplier));
    }

    @ApiOperation("根据条件查供应商")
    @GetMapping("")
    public ResponseEntity<Page<Supplier>> findSupplier(@ApiParam("供货商名称") String name,
                                                       @ApiParam("供货商电话") String tel,
                                                       @ApiParam("页码") @RequestParam(defaultValue = "0") Integer pageNum,
                                                       @ApiParam("每页记录数") @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Supplier> result = supplierService.findSuppliers(name, tel, new Page<>(pageNum, pageSize));
        if(result.getSize() == 0) {
            return ResponseEntity.noContent().build();
        }
        else {
            return ResponseEntity.ok(result);
        }

    }
//    @ApiOperation("根据商品ID查找供应商")
//    @GetMapping()

    @ApiOperation("删除供应商")
    @DeleteMapping("/{id}")
    public void deleteSupplier(@PathVariable long id) throws ProductAdminException {
        supplierService.deleteSupplier(id);
    }

    @ApiOperation("修改供应商信息")
    @PutMapping("")
    public void updateSupplier(@RequestBody Supplier supplier) throws ProductAdminException{
        supplierService.updateSupplier(supplier);
    }
}

